﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier.utils;
using Cashier_Utils;

namespace Cashier
{
    public partial class PrintColumnsForm : Form
    {
        private MainForm mainform;
        private DataGridView dgv;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="dgv"></param>
        public PrintColumnsForm(MainForm mainform, DataGridView dgv)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.dgv = dgv;

            this.GenerateColumns();
            this.BindData();
        }

        /// <summary>
        /// 生成DataGridView列
        /// </summary>
        private void GenerateColumns()
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("valueMember", null, true, 0, null, null, false, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewCheckBoxColumn("选择", false, 20, ActKey.CENTER, ActKey.CENTER, true));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("displayMember", "名称", true, 80, ActKey.LEFT, ActKey.LEFT, true, false));
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            if (dgv != null)
            {
                List<DataItem> list = new List<DataItem>();
                foreach (DataGridViewColumn column in this.dgv.Columns)
                {
                    if (!String.IsNullOrEmpty(column.HeaderText) && !column.HeaderText.Equals(column.Name))
                    {
                        list.Add(DataItem.GetDataItem(column.HeaderText, column.Name));
                    }
                }
                this.dataGridView1.DataSource = list;
            }
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrintColumnsForm_Load(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in this.dataGridView1.Rows)
            {
                bool visible = this.dgv.Columns[row.Cells[0].Value.ToString()].Visible;
                row.Cells[1].Value = visible;
            }
        }

        /// <summary>
        /// 单元格内容单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 1)
            {
                bool visible = Boolean.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString());
                String columnname = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                this.dgv.Columns[columnname].Visible = visible;
            }
        }
    }
}
